<?php
include_once("ConnectionManager.php");

function getAllPage(){
    $query = "SELECT PAG_ID, PAG_NAME, PAG_ORDER FROM MITI_PAGE ORDER BY PAG_ORDER, PAG_NAME"; 
    ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();
}

function getPage($page_id){
    $query = "SELECT PAG_ID, PAG_NAME, PAG_ORDER FROM MITI_PAGE WHERE PAG_ID = $page_id"; 
    ConnectionManager::getInstance()->setQuery($query);
    $page = mysql_fetch_array(ConnectionManager::getInstance()->query());
    return $page;
}

function createPage($name, $order, $type, $url){
    $query = "INSERT INTO MITI_PAGE(PAG_NAME, PAG_ORDER, PAG_TYPE, PAG_URL) VALUES('$name',$order, 0, '')";
    ConnectionManager::getInstance()->setQuery($query);
    $result = ConnectionManager::getInstance()->query();
    return $result;
}

function updatePage($page,$name,$order,$type, $url){
    $query = "UPDATE MITI_PAGE SET PAG_NAME='$name',PAG_ORDER=$order WHERE PAG_ID = $page";
    ConnectionManager::getInstance()->setQuery($query);
    $result = ConnectionManager::getInstance()->query();
    return $result;
}

function deletePage($page){
    $query = "DELETE FROM MITI_PAGE WHERE PAG_ID = $page";
    ConnectionManager::getInstance()->setQuery($query);
    $result = ConnectionManager::getInstance()->query();
    return $result;
}

function hasArticle($page){
    $query = "SELECT COUNT('PAG_ID') FROM MITI_ARTICLE WHERE PAG_ID = $page";
	ConnectionManager::getInstance()->setQuery($query);
    $result = ConnectionManager::getInstance()->query();
    $row1 = mysql_fetch_array($result);
    return $row1[0] > 0;
}

function getAllArticle(){
    $query = "SELECT ART_ID, ART_TITLE, ART_CONTENT, ART_CREATIONDATE,ART_SMALLIMAGE, (SELECT PAG_NAME FROM MITI_PAGE P WHERE P.PAG_ID = A.PAG_ID) AS PAG_NAME, (SELECT PAG_ID FROM MITI_PAGE P WHERE P.PAG_ID = A.PAG_ID) AS PAG_ID FROM  MITI_ARTICLE A ORDER BY PAG_ID, ART_CREATIONDATE";
    ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();
}

function getArticle($article_id){
    $query = "SELECT ART_ID, ART_TITLE, ART_CONTENT, ART_SMALLIMAGE, ART_CREATIONDATE, PAG_ID FROM MITI_ARTICLE WHERE ART_ID = $article_id";
    ConnectionManager::getInstance()->setQuery($query);
    $article = mysql_fetch_array(ConnectionManager::getInstance()->query());
    return $article;
}

function getArticleOfPage($page_id, $start_record, $qty){
    if(isset($start_record)){
        $query = "SELECT ART_ID, ART_TITLE, ART_CONTENT, ART_CREATIONDATE,ART_SMALLIMAGE, (SELECT PAG_NAME FROM MITI_PAGE P WHERE P.PAG_ID = A.PAG_ID) AS PAG_NAME, (SELECT PAG_ID FROM MITI_PAGE P WHERE P.PAG_ID = A.PAG_ID) AS PAG_ID FROM  MITI_ARTICLE A WHERE PAG_ID = $page_id ORDER BY PAG_ID, ART_CREATIONDATE DESC LIMIT ".($start_record-1).", $qty";    
    }else{
        $query = "SELECT ART_ID, ART_TITLE, ART_CONTENT, ART_CREATIONDATE,ART_SMALLIMAGE, (SELECT PAG_NAME FROM MITI_PAGE P WHERE P.PAG_ID = A.PAG_ID) AS PAG_NAME, (SELECT PAG_ID FROM MITI_PAGE P WHERE P.PAG_ID = A.PAG_ID) AS PAG_ID FROM  MITI_ARTICLE A WHERE PAG_ID = $page_id ORDER BY PAG_ID, ART_CREATIONDATE DESC";
    }
    ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();
}

function createArticle($title, $page, $content, $thumbnail){
    $now = new DateTime();
    $encode_title = mysql_real_escape_string($title);
    $encode_content = mysql_real_escape_string($content);
    $query = "INSERT INTO MITI_ARTICLE(ART_TITLE, ART_CONTENT, PAG_ID, ART_CREATIONDATE, ART_SMALLIMAGE) VALUES('$title','$content', $page, '".$now->format('Y-m-d H:i:s')."','$thumbnail')";
    ConnectionManager::getInstance()->setQuery($query);
    $result = ConnectionManager::getInstance()->query();
    return $result;
}

function updateArticle($title, $page, $content, $article, $thumbnail){
    $query = "UPDATE MITI_ARTICLE SET ART_TITLE='$title',ART_CONTENT='$content', PAG_ID=$page, ART_SMALLIMAGE='$thumbnail' WHERE ART_ID = $article";
    ConnectionManager::getInstance()->setQuery($query);
    $result = ConnectionManager::getInstance()->query();
    return $result;
}

function deleteArticle($article){
    $query = "DELETE FROM MITI_ARTICLE WHERE ART_ID = $article";
    ConnectionManager::getInstance()->setQuery($query);
    $result = ConnectionManager::getInstance()->query();
    return $result;
}

function getPageOfArticle($article){
    $query = "SELECT P.PAG_ID, P.PAG_NAME FROM MITI_PAGE P WHERE PAG_ID = (SELECT PAG_ID FROM MITI_ARTICLE WHERE ART_ID = $article)";
    ConnectionManager::getInstance()->setQuery($query);
    $page = mysql_fetch_array(ConnectionManager::getInstance()->query());
    return $page;
}


?>